package com.woniu.his.sms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.his.exception.HisException;
import com.woniu.his.sms.common.SmsResultCode;
import com.woniu.his.sms.common.enums.SmsStatusEnums;
import com.woniu.his.sms.infra.model.RegistrationRank;
import com.woniu.his.sms.infra.mapper.RegistrationRankMapper;
import com.woniu.his.sms.infra.model.Staff;
import com.woniu.his.sms.service.RegistrationRankService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.his.sms.service.StaffService;
import com.woniu.his.sms.service.dto.PageRegistrationRankDto;
import com.woniu.his.sms.service.dto.RegistrationRankDto;
import com.woniu.his.sms.service.param.AddRegistrationRankParam;
import com.woniu.his.sms.service.param.QueryRegistrationRankParam;
import com.woniu.his.sms.service.param.UpdateRegistrationRankParam;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 挂号等级表 服务实现类
 * </p>
 *
 * @author cb
 * @since 2022-11-25
 */
@Service
public class RegistrationRankServiceImpl extends ServiceImpl<RegistrationRankMapper, RegistrationRank> implements RegistrationRankService {

    @Resource
    private StaffService staffService;

    @Override
    public void addRegistrationRank(AddRegistrationRankParam param) {
        RegistrationRank registrationRank = BeanUtil.copyProperties(param, RegistrationRank.class);
        registrationRank.setRegistrationRankStatus(SmsStatusEnums.ON.getCode());
        save(registrationRank);
    }

    @Override
    public void deleteRegistrationRankByIds(List<Long> rankIds) {
        List<Staff> staffList = staffService.list(new QueryWrapper<Staff>().in("registration_rank_id", rankIds));
        if(staffList!=null||staffList.size()>0) throw new HisException(SmsResultCode.REGISTRATION_RANK_USED);
        removeByIds(rankIds);
    }

    @Override
    public PageRegistrationRankDto queryRegistrationRank(QueryRegistrationRankParam param) {
        QueryWrapper<RegistrationRank> queryWrapper = new QueryWrapper<>();
        //根据编码号查
        if(param.getRegistrationRankCode()!=null&&!param.getRegistrationRankCode().trim().equals("")){
            queryWrapper.and(q->q.like("registration_rank_code",param.getRegistrationRankCode()));
        }
        //根据挂号等级别名查
        if(param.getRegistrationRankName()!=null&&!param.getRegistrationRankName().trim().equals("")){
            queryWrapper.and(q->q.like("registration_rank_name",param.getRegistrationRankName()));
        }
        //根据挂号等级状态查
        if(param.getRegistrationRankStatus().equals(SmsStatusEnums.ON.getCode())||param.getRegistrationRankStatus().equals(SmsStatusEnums.OFF.getCode())){
            queryWrapper.eq("registration_rank_status",param.getRegistrationRankStatus());
        }
        //根据价格区间查
        if(param.getStartPrice()==null) param.setStartPrice(new BigDecimal(0.00));
        if(param.getEndPrice()==null) {
            List<RegistrationRank> ranks = list(new QueryWrapper<RegistrationRank>().orderByDesc("price"));
            param.setEndPrice(ranks.get(0).getPrice());
        }
        queryWrapper.between("price",param.getStartPrice(),param.getEndPrice());
        //根据排序号查
        if(param.getSeqNo()!=null) queryWrapper.eq("seq_no",param.getSeqNo());
        Page<RegistrationRank> page = page(new Page<>(param.getCurrent(), param.getSize()), queryWrapper);
        PageRegistrationRankDto pageRegistrationRankDto = new PageRegistrationRankDto();
        pageRegistrationRankDto.setCurrent(page.getCurrent());
        pageRegistrationRankDto.setSize(page.getSize());
        pageRegistrationRankDto.setTotal(page.getTotal());
        pageRegistrationRankDto.setRankDtos(BeanUtil.copyToList(page.getRecords(),RegistrationRankDto.class));
        return pageRegistrationRankDto;
    }

    @Override
    public List<RegistrationRankDto> queryAllRegistrationRank() {
        List<RegistrationRank> ranks = list();
        return BeanUtil.copyToList(ranks,RegistrationRankDto.class);
    }

    @Override
    public RegistrationRankDto updateRegistrationRank(UpdateRegistrationRankParam param) {
        RegistrationRank registrationRank = BeanUtil.copyProperties(param, RegistrationRank.class);
        updateById(registrationRank);
        return BeanUtil.copyProperties(registrationRank,RegistrationRankDto.class);
    }
}
